//13.1 - The Random Color Program -Dirk Henkemans #include #include #include #include LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam); LPDIRECTDRAW7 g_pdd; //the DirectDraw object LPDIRECTDRAWSURFACE7 g_pddsprimary; //the primary surface LPDIRECTDRAWSURFACE7 g_pddsback; //the back buffer LPDIRECTDRAWSURFACE7 g_pddsone; //a temporary surface DDSURFACEDESC2 ddsd; //used to store surface descriptions DDSCAPS2 ddsc; //stores the capabilities of a surface HRESULT hRet; //used to temporarily store the result of a function int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow) { HWND hWnd; MSG msg; WNDCLASSEX wc; //fill the WNDCLASSEX structure with the appropriate values wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInst; wc.hIcon = LoadIcon(NULL, IDI_EXCLAMATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "RandomColor"; wc.hIconSm = LoadIcon(NULL, IDI_EXCLAMATION); //register the new class RegisterClassEx(&wc); //create a window hWnd = CreateWindowEx( NULL, "RandomColor", "The Random Color Program", WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL ); //event loop - handle all messages while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } //standard return value return (msg.wParam); } LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { //find out which message is being sent switch(nMsg) { case WM_CREATE: //create the timer (3 seconds) SetTimer(hWnd, 1, 3000, NULL); //create the DirectDraw object hRet = DirectDrawCreateEx(NULL, (void**)&g_pdd,IID_IDirectDraw7, NULL); if(hRet != DD_OK) MessageBox(hWnd, "DirectDrawCreateEx Failed", "Error", NULL); //Set the Cooperative Level hRet = g_pdd->SetCooperativeLevel(hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE); if(hRet != DD_OK) MessageBox(hWnd, "SetCooperativeLevel Failed", "Error", NULL);; //Set the display mode: 800x600 with 16 bits per pixel hRet = g_pdd->SetDisplayMode(800, 600, 16,0,0); if(hRet != DD_OK) MessageBox(hWnd, "SetDisplayMode Failed", "Error", NULL);; //prepare primary surface info ZeroMemory(&ddsd,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.dwBackBufferCount = 1; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; //create the surface hRet = g_pdd->CreateSurface(&ddsd, &g_pddsprimary, NULL); if (hRet != DD_OK) MessageBox(hWnd, "CreateSurface Failed", "Error", NULL); //prepare the back buffer info ZeroMemory(&ddsc,sizeof(ddsc)); ddsc.dwCaps = DDSCAPS_BACKBUFFER; //get a pointer to the back buffer hRet = g_pddsprimary->GetAttachedSurface(&ddsc,&g_pddsback); srand(time(0)); break; case WM_TIMER: //when the timer goes off (only one) DDBLTFX ddbltfx; ZeroMemory(&ddbltfx,sizeof(ddbltfx)); ddbltfx.dwSize = sizeof(ddbltfx); ddbltfx.dwFillColor = rand()%16; g_pddsback->Blt(NULL,NULL,NULL, DDBLT_COLORFILL, &ddbltfx); g_pddsprimary->Flip(NULL,0); break; case WM_DESTROY: //destroy the timer KillTimer(hWnd, 1); //end the program PostQuitMessage(0); break; default: //let Windows handle every other message return(DefWindowProc(hWnd, nMsg, wParam, lParam)); } return 0; }